package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
/**
 *
 * 7985
 * http://hongdou.gxnews.com.cn/ 红豆社区
 * @author KK
 */
public class GxnewsBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://hongdou.gxnews.com.cn/viewthread-[0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news = new NewsMeta();
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		String updateUrl = url;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);


		title = doc.select("title").text();
		if (title!=null) {
			title = StringUtils.substringBeforeLast(title, "-");
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("div.thead").text();
		}

		Element firstDiv = doc.select("div.postbit").first();
		if (firstDiv!=null){
			date = DateUtils.matchDate(firstDiv.select(":containsOwn(发表于)").text());
			Elements element = firstDiv.select("td.alt1");
			element.select("table,.leftmenu,div[style*=display: none]").remove();
			element.select("td.alt1").remove();
			content = HtmlCleaner.getContentHtml(url,  element);
			author = firstDiv.select("span.green").text();
			String commTxt = doc.select("span.normalinfo").text();
			if(commTxt!=null){
				clickNum= NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "|"));
				commnetNum= NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "|"));
			}
		}

		if (StringUtils.isBlank(author)){
			Element first = doc.select("div[id^=postmenu_]").first();
			if (first!=null){
				author = first.text();
			}
		}

		if (clickNum==null && commnetNum==null){
			/*
			 * 解析点击数和评论数
			 */
			try {
				String msgStr = doc.select("span.normalinfo").text();
				String clickNumStr = StringUtils.substringBetween(msgStr, "第", "个");
				if(StringUtils.isNotBlank(clickNumStr)){
					clickNumStr = clickNumStr.trim();
				}
				clickNum = Integer.valueOf(clickNumStr);
				String commentNumStr = StringUtils.substringBetween(msgStr, "本帖回复:", ")");
				if(StringUtils.isNotBlank(commentNumStr)){
					commentNumStr = commentNumStr.trim();
				}
				commnetNum = Integer.valueOf(commentNumStr);
			} catch (Exception e) {
			}
		}


		news.setUrl(url);
		news.setType(4);
		news.setAuthor(author);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(commnetNum);
		news.setClickNum(clickNum);
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setUpdateUrl(updateUrl);
		return news;

	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	

}
